package com.gzhryc.pay.adaptation.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;

import java.util.concurrent.ConcurrentHashMap;

public class AliHttpClient {

    static Logger log = Logger.getLogger(AliHttpClient.class);

    private static String charset = "UTF-8";
    private static ConcurrentHashMap<String, HTTPClientEntity> httpClientMap = new ConcurrentHashMap<>();

    public static DefaultAlipayClient getHttpClient(DefaultAlipayConfig config) throws LogicException {
        AliHttpClient.HTTPClientEntity entity = httpClientMap.get(config.getAppId());
        if(entity != null && StringTools.isNotBlank(config.getCertificatePath())){
            if(entity.getCertificatePath() == null){
                entity = null;
            }
        }
        if (entity == null) {
            if (StringTools.isNotBlank(config.getAppId()) && StringTools.isNotBlank(config.getAppPrivateKey())){
                if(StringTools.isNotBlank(config.getCertificatePath())){
                    /** 公钥证书方式 转账需要 ***/
                    CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
                    certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
                    certAlipayRequest.setAppId(config.getAppId());
                    certAlipayRequest.setPrivateKey(config.getAppPrivateKey());
                    certAlipayRequest.setFormat("json");
                    certAlipayRequest.setCharset("UTF-8");
                    certAlipayRequest.setSignType("RSA2");
                    certAlipayRequest.setCertPath(config.getCertificatePath() + config.getAppId() + java.io.File.separator + "appCertPublicKey.crt");
                    certAlipayRequest
                            .setAlipayPublicCertPath(config.getCertificatePath() + config.getAppId() + java.io.File.separator + "alipayCertPublicKey_RSA2.crt");
                    certAlipayRequest.setRootCertPath(config.getCertificatePath() + config.getAppId() + java.io.File.separator + "alipayRootCert.crt");
                    try {
                        DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
                        entity = new HTTPClientEntity(alipayClient,config.getCertificatePath());
                    } catch (AlipayApiException e) {
                        log.error(e.getMessage(), e);
                    }
                }else if(StringTools.isNotBlank(config.getAlipayPublicKey())){
                    DefaultAlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", config.getAppId(), config.getAppPrivateKey(), "json",
                            charset, config.getAlipayPublicKey(), "RSA2");
                    entity = new HTTPClientEntity(alipayClient,config.getCertificatePath());
                }
                //缓存HTTP客户端
                httpClientMap.put(config.getAppId(),entity);
            }
        }
        if(entity == null){
            throw new LogicException("初始化"+config.getAppId()+"支付宝HTTP服务失败");
        }
        return entity.client;
    }

    public static class HTTPClientEntity {

        DefaultAlipayClient client;
        String certificatePath;

        public HTTPClientEntity(){}

        public HTTPClientEntity(DefaultAlipayClient client){
            this.client = client;
        }

        public HTTPClientEntity(DefaultAlipayClient client,String certificatePath){
            this.client = client;
            this.certificatePath = certificatePath;
        }

        public DefaultAlipayClient getClient() {
            return client;
        }

        public void setClient(DefaultAlipayClient client) {
            this.client = client;
        }

        public String getCertificatePath() {
            return certificatePath;
        }

        public void setCertificatePath(String certificatePath) {
            this.certificatePath = certificatePath;
        }
    }
}
